home *** CD-ROM | disk | FTP | other *** search
- Path: news.mindspring.com!usenet
- From: rudd@mindspring.com (Justin Rudd)
- Newsgroups: comp.lang.c++
- Subject: Re: Can you overload a constructor?
- Date: Fri, 15 Mar 1996 17:22:39 GMT
- Organization: MindSpring Enterprises
- Message-ID: <4ic92p$lj6@B1FF.mindspring.com>
- References: <Do859K.K9B@watserv3.uwaterloo.ca> <31483CDC.4B0C@staff.ichange.com> <4ibqan$br5@si-nic.hrz.uni-siegen.de>
- Reply-To: rudd@mindspring.com
- NNTP-Posting-Host: rudd.mindspring.com
- X-Newsreader: Forte Free Agent v0.55
-
- plrunu@informatik.uni-siegen.de (Runu Knips) wrote:
-
- >Sorry, it is impossible to OVERLOAD a constructor. A constructor
- >is ALWAYS called, if there is one for a class. And before it
- >get called, any constructor for superclasses get called.
-
- um...actually you can overload constructors. Of course they do get
- called but not always. But first lets looks at this...
-
- class Foo
- {
- public:
- Foo();
- Foo( int i );
- Foo( float f );
- Foo( double d );
-
- void Get( int& i );
- void Get( float& f );
- void Get( double& d );
-
- };
-
- There I have 4 different OVERLOADED ways to initialize an object of
- type Foo. And also 3 different OVERLOADED ways to get the value of
- Foo.
-
- Now as for constructors always being called. What if I have this:
-
- class Graphic
- {
- public:
- virtual void Draw() = 0;
- };
-
- class Box : public Graphic
- {
- public:
- virtual void Draw();
- };
-
- void main()
- {
- Graphic* graph;
- Box box;
-
- graph = &box;
-
- graph->Draw();
- }
-
- In this little code segment here I have a POINTER to a Graphic object.
- It has not been initialized..therefore no constructor call. And then
- after that...I make the Graphic pointer point to a Box object...so at
- no time was the Graphic pointer an instance of Graphic.
-
- Justin
-
-